package cn.gcyl.module.biz.enums.sender;

import cn.gcyl.framework.common.util.json.JsonUtils;
import cn.gcyl.module.biz.dal.dataobject.SysMemberUserDO;
import cn.gcyl.module.biz.dal.dataobject.thirdpartyinformation.ThirdPartyInformationDO;
import cn.gcyl.module.biz.dal.mysql.SysMemberUserMapper;
import cn.gcyl.module.biz.service.thirdpartyinformation.ThirdPartyInformationService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 第三方信息定时获取任务
 */
@Component
@Slf4j
public class UserMemberStatusSender {

    @Resource
    private SysMemberUserMapper sysMemberUserMapper;

    /**
     * 判断用户会员状态
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void memberStatusSender() {
        LocalDateTime now = LocalDateTime.now();
        log.info("开始执行用户会员状态定时任务，时间: {}",now);
        // 查询memberId不为空且不为0的用户（即已开通会员的用户）
        List<SysMemberUserDO> memberUsers = sysMemberUserMapper.selectList(
                new LambdaQueryWrapper<SysMemberUserDO>()
                        .isNotNull(SysMemberUserDO::getMemberId)
                        .ne(SysMemberUserDO::getMemberId, 0)
        );

        if (ObjectUtil.isEmpty(memberUsers)) {
            log.info("未找到已开通会员的用户");
            return;
        }
        for (SysMemberUserDO sysMemberUserDO : memberUsers) {
            if (sysMemberUserDO.getRemainingConsultationFrequency()==0 &&
                    sysMemberUserDO.getRemainingRegistrationsFrequency()==0 &&
                    sysMemberUserDO.getMemberExpirationDate().isBefore(now)
            ){
                sysMemberUserDO.setMemberId(null);
                sysMemberUserMapper.updateById(sysMemberUserDO);
            }
        }

    }




}